//
//  JaneRadioPlayController.m
//  EPA
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 张静. All rights reserved.
//

#import "JaneRadioPlayController.h"
#import "JaneRadioListModel.h"
#import "JaneAVManager.h"
@interface JaneRadioPlayController ()
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//封面
@property (weak, nonatomic) IBOutlet UIImageView *coverimgView;
//当前播放时间
@property (weak, nonatomic) IBOutlet UILabel *currentLabel;
//播放进度
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
//总时间
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation JaneRadioPlayController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    JaneRadioListModel *model = self.radioArray[self.currentIndex];
    self.titleLabel.text = model.title;
    [self.coverimgView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    [JaneAVManager  shareManager].currentIndex = self.currentIndex;
    NSMutableArray *itemArray = [NSMutableArray new];
    for (JaneRadioListModel *model in self.radioArray) {
        NSURL *musicUrl = [NSURL URLWithString:model.playInfoModel.musicUrl];
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:musicUrl];
        [itemArray addObject:item];
    }
//    AVPlayerItem数组
    [JaneAVManager shareManager].itemArray = itemArray;
    [[JaneAVManager shareManager] play];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)didChangeRateAction:(UISlider *)sender {
    CMTime changeTime = CMTimeMake(sender.value*[JaneAVManager shareManager].player.currentItem.duration.value,[JaneAVManager shareManager].player.currentItem.duration.timescale);
    [[JaneAVManager shareManager].player seekToTime:changeTime];
}
- (void)timerAction {
    float current = [JaneAVManager shareManager].player.currentTime.value / [JaneAVManager shareManager].player.currentTime.timescale;
    NSString *currentStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)current/60,(NSInteger)current%60];
    self.currentLabel.text = currentStr;
    float total = 0;
    if ([JaneAVManager shareManager].player.currentItem.duration.timescale > 0) {
        total = [JaneAVManager shareManager].player.currentItem.duration.value / [JaneAVManager shareManager].player.currentItem.duration.timescale;
    }
    NSString *totalStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)total/60,(NSInteger)total%60];
    self.totalLabel.text = totalStr;
    if (total > 0) {
        float rate = current/total;
        self.rateSlider.value = rate;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//上一曲
- (IBAction)didClickPreviousButton:(id)sender {
    [[JaneAVManager shareManager] previous];
//    图片和标题
    self.currentIndex--;
    if (self.currentIndex < 0) {
        self.currentIndex = self.radioArray.count  - 1;
    }
    JaneRadioListModel *model = self.radioArray[self.currentIndex];
    [self.coverimgView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
    [self.playButton setImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
}
// 播放 暂停
- (IBAction)didClickPlayButton:(id)sender {
    if ([JaneAVManager shareManager].player.rate == 0) {
        [[JaneAVManager shareManager] play];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
    }else {
        [[JaneAVManager shareManager] pause];
         [(UIButton *)sender setImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
    }
}
//下一曲
- (IBAction)didClickNextButton:(id)sender {
    [[JaneAVManager shareManager] next];
    self.currentIndex++;
    if (self.currentIndex >= self.radioArray.count) {
        self.currentIndex = 0;
    }
    JaneRadioListModel *model = self.radioArray[self.currentIndex];
    [self.coverimgView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
    [self.playButton setImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
